package cn.joker.config;


import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Configuration
@EnableSwagger2WebMvc
@EnableKnife4j
@Slf4j
public class Knife4jConfiguration {
    @Value("${server.port}")
    private String port;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.joker.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        try {
            InetAddress ip4 = Inet4Address.getLocalHost();
            log.info("API地址：http://{}:{}/auth/doc.html", ip4.getHostAddress(), port);
        } catch (UnknownHostException e) {
           log.error(e.getMessage());
        }
        return new ApiInfoBuilder()
                .title("权限控制")
                .description("权限控制")
                .termsOfServiceUrl("http://127.0.0.1:8808/")
                .version("1.0").contact(new Contact("ensure", "http://127.0.0.1:8808/doc.html", "xxx@gmail.com")).license("abcdefg")
                .build();
    }
}